package com.health.viewscontrols.tjbg;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.ImageUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.health.R;
import com.health.tablebean.ARCHIVE_BASEINFO;
import com.health.tablebean.SBCL_DATA;
import com.health.tools.AssetUtil;
import com.health.tools.FileUtils;
import com.health.tools.GlobalConts;
import com.health.viewscontrols.NovaApplication;
import com.health.viewscontrols.tjbg.adapter.TjbgDataAdapter;
import com.health.viewscontrols.tjbg.base.NovaPtActivity;
import com.health.viewscontrols.tjbg.bean.TjDataBean;
import com.health.viewscontrols.tjbg.bean.XuezhiBean;
import com.health.viewscontrols.tjbg.fragment.DanguchunFragment;
import com.health.viewscontrols.tjbg.fragment.DhRTCFFragment;
import com.health.viewscontrols.tjbg.fragment.NiaoSuanFragment;
import com.health.viewscontrols.tjbg.fragment.PEFReportFragment;
import com.health.viewscontrols.tjbg.fragment.TiWenFragment;
import com.health.viewscontrols.tjbg.fragment.TiZhongFragment;
import com.health.viewscontrols.tjbg.fragment.XinLvFragment;
import com.health.viewscontrols.tjbg.fragment.XueTangFragment;
import com.health.viewscontrols.tjbg.fragment.XueYaFragment;
import com.health.viewscontrols.tjbg.fragment.XueYangFragment;
import com.health.viewscontrols.tjbg.fragment.XuezhiFragment;
import com.peng.ppscalelibrary.BleManager.Model.LFPeopleGeneral;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;

/**
 * author : xiaoxin
 * date   : 2020/12/21 9:32
 * desc   :
 * update :
 */
public class TjbgActivity extends NovaPtActivity {

    private static final String TAG = "TjbgActivity";
    private TextView tv_tjbg_name,tv_tjbg_sex,tv_tjbg_age,tv_tjbg_csrq,tv_tjbg_address,tv_tjbg_rqfl
            ,tv_khxs,tv_yx,tv_tt,tv_bxb,tv_yxsy,tv_ndy,tv_dbz,tv_dhs,tv_ptt,tv_bz,tv_ph,tv_tjbg_egg_nodata
            ,tv_ny_time,tv_tjbg_egg,tv_tjbg_egg_time;
    private ImageView iv_tjbg_egg;
    private RecyclerView rv_tj_data;
    private TjbgDataAdapter tjbgDataAdapter;
    private List<TjDataBean> dataBeanList = new ArrayList<>();
    public ARCHIVE_BASEINFO userInfo;
    String strWhere = "IDCARD = '" + NovaApplication.getUserInfo().getIDCARD() + "'";
    private LinearLayout tjbgView;

    private Bitmap createBitmap() {
        int densityDpi = getResources().getDisplayMetrics().densityDpi;
        int width = (int) ((densityDpi / 2.54) * 21);
//        int height = (int) ((densityDpi / 2.54) * 29.7);
        int height = (int) ((densityDpi / 2.54) * 30);
        Log.e("xiaoxin", "createBitmap: " +  width);
        Log.e("xiaoxin", "createBitmap: " +  height);
//        ImageView imageView = tjbgView.findViewById(R.id.iv_code1);
//        imageView.setImageBitmap(zxing1(qRCodeUrl));
        tjbgView.measure(View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY),
                View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.EXACTLY));
        tjbgView.layout(0, 0, tjbgView.getMeasuredWidth(), tjbgView.getMeasuredHeight());

        Bitmap bitmap = Bitmap.createBitmap(
                tjbgView.getMeasuredWidth(),
                tjbgView.getMeasuredHeight(),
                Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);

        canvas.drawColor(Color.WHITE);
        tjbgView.draw(canvas);
        return Bitmap.createScaledBitmap(bitmap, width, height, true);
    }

    @Override
    public void initData(@Nullable Bundle bundle) {

    }

    @Override
    public int bindLayout() {
        return R.layout.activity_tjbg;
    }

    @Override
    public void initView(Bundle savedInstanceState, View contentView) {

        findViewById(R.id.ll_finish).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
//        findViewById(R.id.bt_save).setVisibility(View.GONE);
        Button bt_save = findViewById(R.id.bt_save);
        bt_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showProgressDialog("请稍等，正在生成报告");
                FileUtils.isPathExist();
                String filePath = FileUtils.PATH_ROOT
                        + userInfo.getCUSTOMERNAME()
                        + "_1"
//                        + TimeUtils.date2String(new Date(),"yyyy-MM-dd HH:mm:ss")
                        + ".png";

                Bitmap oldBitmap = createBitmap();
                int width = oldBitmap.getWidth();
                int height = oldBitmap.getHeight();

                int textSize = 25;
//                String text = "- 第一页 -";
                String text = "";
                int textWidth = text.length() * textSize;
                float x = (width / 2f) - (textWidth / 2);
                float y = height - 90f;

                Bitmap bitmap = ImageUtils.addTextWatermark(oldBitmap, text, textSize, Color.BLACK, x, y);
                ImageUtils.save(bitmap, filePath, Bitmap.CompressFormat.PNG);

//                if (scl90List != null && scl90List.size() > 0) {
//                    saveScl90Report();
//                }

                cancelProgressDialog();
                // 通知相册刷新文件
                File file = new File(filePath);
                MediaStore.Images.Media.insertImage(getContentResolver(),
                        BitmapFactory.decodeFile(file.getAbsolutePath()), file.getName(), null);
//                webview.setVisibility(View.GONE);

                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        showTextDialog("保存成功");
//                        webview.setVisibility(View.VISIBLE);
                    }
                },500);
                try {
                    Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
                    Uri uri = Uri.fromFile(file);
                    intent.setData(uri);
                    sendBroadcast(intent);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        TextView tv_title = findViewById(R.id.tv_title);
        tv_title.setText("体检报告");

        tjbgView = findViewById(R.id.ll_tjbg);
        tv_tjbg_name = findViewById(R.id.tv_tjbg_name);
        tv_tjbg_sex = findViewById(R.id.tv_tjbg_sex);
        tv_tjbg_age = findViewById(R.id.tv_tjbg_age);
        tv_tjbg_csrq = findViewById(R.id.tv_tjbg_csrq);
        tv_tjbg_address = findViewById(R.id.tv_tjbg_address);
        tv_tjbg_rqfl = findViewById(R.id.tv_tjbg_rqfl);
        rv_tj_data = findViewById(R.id.rv_tj_data);
        tv_khxs = findViewById(R.id.tv_khxs);
        tv_yx = findViewById(R.id.tv_yx);
        tv_tt = findViewById(R.id.tv_tt);
        tv_bxb = findViewById(R.id.tv_bxb);
        tv_yxsy = findViewById(R.id.tv_yxsy);
        tv_ndy = findViewById(R.id.tv_ndy);
        tv_dbz = findViewById(R.id.tv_dbz);
        tv_dhs = findViewById(R.id.tv_dhs);
        tv_ptt = findViewById(R.id.tv_ptt);
        tv_bz = findViewById(R.id.tv_bz);
        tv_ph = findViewById(R.id.tv_ph);
//        iv_tjbg_egg = findViewById(R.id.iv_tjbg_egg);
//        tv_tjbg_egg_nodata = findViewById(R.id.tv_tjbg_egg_nodata);
        tv_ny_time = findViewById(R.id.tv_ny_time);
        tv_tjbg_egg = findViewById(R.id.tv_tjbg_egg);
        tv_tjbg_egg_time = findViewById(R.id.tv_tjbg_egg_time);
    }

    @Override
    public void doBusiness() {

        setTextNy();
        egg();
        setTextData();
        initAdapter();

//        血压 50-180
//        血糖 0-12
//        体温 34-42
//        体重 20-260
//        血氧 0-100
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(R.id.xueya, XueYaFragment.newInstance());
        transaction.replace(R.id.xuetang, XueTangFragment.newInstance());
        transaction.replace(R.id.tiwen, TiWenFragment.newInstance());
        transaction.replace(R.id.tizhong, TiZhongFragment.newInstance());
        transaction.replace(R.id.xueyang, XueYangFragment.newInstance());
        transaction.replace(R.id.xinlv, XinLvFragment.newInstance());
        transaction.replace(R.id.danguchun, DanguchunFragment.newInstance());
        transaction.replace(R.id.niaosuan, NiaoSuanFragment.newInstance());
        transaction.replace(R.id.xuezhi, XuezhiFragment.newInstance());
        transaction.replace(R.id.rentichengfen, DhRTCFFragment.newInstance());
        transaction.replace(R.id.pef, PEFReportFragment.newInstance());
        transaction.commit();

    }

    private void setTextData() {
        try {
            userInfo = NovaApplication.getUserInfo();
            tv_tjbg_name.setText(userInfo.getCUSTOMERNAME()+"");

//            String sex = userInfo.getSEX() == "DM01-08_1" ? "男" : "女";
//            Log.e(TAG, "setTextData: " + userInfo.getSEX() );
//            Log.e(TAG, "setTextData: " + sex );
            if ("DM01-08_1".equals(userInfo.getSEX())) {
                tv_tjbg_sex.setText("男");
            } else if ("DM01-08_2".equals(userInfo.getSEX())) {
                tv_tjbg_sex.setText("女");
            } else {
                tv_tjbg_sex.setText("未知性别");
            }

            int age = GlobalConts.getAge(userInfo.getBIRTHDAY());

            tv_tjbg_age.setText("" + age);
            tv_tjbg_address.setText(userInfo.getADDR());
            tv_tjbg_rqfl.setText(AssetUtil.getPeopleType(userInfo.getPOPULATION_TYPE()));
            tv_tjbg_csrq.setText(TimeUtils.millis2String(userInfo.getBIRTHDAY().getTime(),"yyyy-MM-dd"));
        } catch (Exception e) {
            e.printStackTrace();
        }

        Log.e(TAG, "doBusiness: " + userInfo.toString() );
    }

    private void initAdapter() {
        try {
            List<SBCL_DATA> gaoya = NovaApplication.fdbmanger.findAllByWhere(SBCL_DATA.class,
                    strWhere + " AND CL_TYPE = '高压' ORDER BY CL_TIME DESC");
            List<SBCL_DATA> diya = NovaApplication.fdbmanger.findAllByWhere(SBCL_DATA.class,
                    strWhere + " AND CL_TYPE = '低压' ORDER BY CL_TIME DESC");

            if (gaoya != null && gaoya.size() > 0) {
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.append(gaoya.get(0).getCL_VALUE().replace("mmHg",""));
                if (diya != null && diya.size() > 0) {
                    stringBuilder.append("/" + diya.get(0).getCL_VALUE().replace("mmHg",""));
                } else {
                    stringBuilder.append("/-");
                }
                dataBeanList.add(new TjDataBean(stringBuilder.toString(),
                        "mmHg","血压",gaoya.get(0).getCL_TIME(), R.drawable.sbcl_xueya0_icon));
            } else {
                dataBeanList.add(new TjDataBean("-/-",
                        "mmHg","血压","", R.drawable.sbcl_xueya0_icon));
            }

            List<SBCL_DATA> xuetang = NovaApplication.fdbmanger.findAllByWhere(SBCL_DATA.class,
                    strWhere + "AND CL_TYPE = '血糖' ORDER BY CL_TIME DESC");
            if (xuetang != null && xuetang.size() > 0) {
                dataBeanList.add(new TjDataBean(xuetang.get(0).getCL_VALUE().replace("mmol/L",""),
                        "mmHg","血糖",xuetang.get(0).getCL_TIME(), R.drawable.sbcl_xuetang0_icon));
            } else {
                dataBeanList.add(new TjDataBean("-", "mmol/L","血糖",
                        "", R.drawable.sbcl_xuetang0_icon));
            }

            List<SBCL_DATA> tiwen = NovaApplication.fdbmanger.findAllByWhere(SBCL_DATA.class,
                    strWhere + "AND CL_TYPE = '体温' ORDER BY CL_TIME DESC");
            if (tiwen != null && tiwen.size() > 0) {
                dataBeanList.add(new TjDataBean(tiwen.get(0).getCL_VALUE().replace("℃",""),
                        "℃","体温",tiwen.get(0).getCL_TIME(), R.drawable.sbcl_tiwen0_icon));
            } else {
                dataBeanList.add(new TjDataBean("-",
                        "℃","体温","", R.drawable.sbcl_tiwen0_icon));
            }

            List<SBCL_DATA> tizhong = NovaApplication.fdbmanger.findAllByWhere(SBCL_DATA.class,
                    strWhere + "AND CL_TYPE = '体重' ORDER BY CL_TIME DESC");
            if (tizhong != null && tizhong.size() > 0) {
                String weightValue = tizhong.get(0).getCL_VALUE();
                if (!TextUtils.isEmpty(weightValue) && weightValue.length() > 10) {
                    try {
                        LFPeopleGeneral lfPeopleGeneral = GsonUtils.fromJson(weightValue, LFPeopleGeneral.class);
                        double lfWeightKg = lfPeopleGeneral.lfWeightKg;
                        dataBeanList.add(new TjDataBean(lfWeightKg + "",
                                "Kg","体重",tizhong.get(0).getCL_TIME(), R.drawable.sbcl_tizhong0_icon));
                    } catch (Exception e) {
                        dataBeanList.add(new TjDataBean(tizhong.get(0).getCL_VALUE().replace("Kg",""),
                                "Kg","体重",tizhong.get(0).getCL_TIME(), R.drawable.sbcl_tizhong0_icon));
                    }
                } else {
                    dataBeanList.add(new TjDataBean(tizhong.get(0).getCL_VALUE().replace("Kg",""),
                            "Kg","体重",tizhong.get(0).getCL_TIME(), R.drawable.sbcl_tizhong0_icon));
                }

            } else {
                dataBeanList.add(new TjDataBean("-",
                        "Kg","体重","", R.drawable.sbcl_tizhong0_icon));
            }

            List<SBCL_DATA> xueyang = NovaApplication.fdbmanger.findAllByWhere(SBCL_DATA.class,
                    strWhere + "AND CL_TYPE = '血氧' ORDER BY CL_TIME DESC");

            if (xueyang != null && xueyang.size() > 0) {
                dataBeanList.add(new TjDataBean(xueyang.get(0).getCL_VALUE(),
                        "","血氧",xueyang.get(0).getCL_TIME(), R.drawable.sbcl_xueyang0_icon));
            } else {
                dataBeanList.add(new TjDataBean("-%",
                        "","血氧","", R.drawable.sbcl_xueyang0_icon));
            }

            List<SBCL_DATA> xinlv = NovaApplication.fdbmanger.findAllByWhere(SBCL_DATA.class,
                    strWhere + "AND CL_TYPE = '心率' ORDER BY CL_TIME DESC");
            if (xinlv != null && xinlv.size() > 0) {
                dataBeanList.add(new TjDataBean(xinlv.get(0).getCL_VALUE()
                        .replace("次/分","")
                        .replace("bpm",""),
                        "bpm","心率",xinlv.get(0).getCL_TIME(), R.drawable.sbcl_xindian0_icon));
            } else {
                dataBeanList.add(new TjDataBean("-",
                        "bpm","心率","", R.drawable.sbcl_xindian0_icon));
            }

            List<SBCL_DATA> dgc = NovaApplication.fdbmanger.findAllByWhere(SBCL_DATA.class,
                    strWhere + "AND CL_TYPE = '胆固醇' ORDER BY CL_TIME DESC");
            if (dgc != null && dgc.size() > 0) {
                dataBeanList.add(new TjDataBean(dgc.get(0).getCL_VALUE()
                        .replace("mmol/L",""),
                        "mmol/L","胆固醇",dgc.get(0).getCL_TIME(), R.drawable.ic_tjbg_dgc));
            } else {
                dataBeanList.add(new TjDataBean("-",
                        "mmol/L","胆固醇","", R.drawable.ic_tjbg_dgc));
            }

            List<SBCL_DATA> ns = NovaApplication.fdbmanger.findAllByWhere(SBCL_DATA.class,
                    strWhere + " AND CL_TYPE = '尿酸' ORDER BY CL_TIME DESC");
            if (ns != null && ns.size() > 0) {
                dataBeanList.add(new TjDataBean(ns.get(0).getCL_VALUE()
                        .replace("μmol/L",""),
                        "μmol/L","尿酸",ns.get(0).getCL_TIME(), R.drawable.ic_tjbg_ns));
            } else {
                dataBeanList.add(new TjDataBean("-",
                        "μmol/L","尿酸","", R.drawable.ic_tjbg_ns));
            }

            List<SBCL_DATA> xuezhi = NovaApplication.fdbmanger.findAllByWhere(SBCL_DATA.class,
                    strWhere + " AND CL_TYPE = '血脂' ORDER BY CL_TIME DESC");
            if (xuezhi != null && xuezhi.size() > 0) {
                XuezhiBean xuezhiBean = null;
                try {
                    xuezhiBean = GsonUtils.fromJson(xuezhi.get(0).getCL_VALUE(), XuezhiBean.class);
                    if (xuezhiBean != null) {
                        dataBeanList.add(new TjDataBean(xuezhiBean.getTc()+"",
                                "mmol/L","总胆固醇(TC)",xuezhi.get(0).getCL_TIME(), R.drawable.ic_tjbg_dgc));

                        dataBeanList.add(new TjDataBean(xuezhiBean.getTg()+"",
                                "mmol/L","甘油三酯(TG)",xuezhi.get(0).getCL_TIME(), R.drawable.ic_tjbg_gysz));

                        dataBeanList.add(new TjDataBean(xuezhiBean.getHdl()+"",
                                "mmol/L","高密度脂蛋白(HDL)",xuezhi.get(0).getCL_TIME(), R.drawable.ic_tjbg_hdl));

                        dataBeanList.add(new TjDataBean(xuezhiBean.getLdl()+"",
                                "mmol/L","低密度脂蛋白(LDL)",xuezhi.get(0).getCL_TIME(), R.drawable.ic_tjbg_ldl));
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {
                dataBeanList.add(new TjDataBean("-",
                        "mmol/L","总胆固醇(TC)","", R.drawable.ic_tjbg_dgc));

                dataBeanList.add(new TjDataBean("-",
                        "mmol/L","甘油三酯(TG)","", R.drawable.ic_tjbg_gysz));

                dataBeanList.add(new TjDataBean("-",
                        "mmol/L","高密度脂蛋白(HDL)","", R.drawable.ic_tjbg_hdl));

                dataBeanList.add(new TjDataBean("-",
                        "mmol/L","低密度脂蛋白(LDL)","", R.drawable.ic_tjbg_ldl));
            }

            tjbgDataAdapter = new TjbgDataAdapter(R.layout.layout_item_tjbg_data_list,dataBeanList);
            rv_tj_data.setLayoutManager(new GridLayoutManager(this,6));
            rv_tj_data.setAdapter(tjbgDataAdapter);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void setTextNy() {
        try {
            List<SBCL_DATA> wssList = NovaApplication.fdbmanger.findAllByWhere(SBCL_DATA.class,
                    strWhere + " AND CL_TYPE = '维生素' ORDER BY CL_TIME DESC");

            List<SBCL_DATA> yxList = NovaApplication.fdbmanger.findAllByWhere(SBCL_DATA.class,
                    strWhere + " AND CL_TYPE = '潜血' ORDER BY CL_TIME DESC");

            List<SBCL_DATA> ttList = NovaApplication.fdbmanger.findAllByWhere(SBCL_DATA.class,
                    strWhere + " AND CL_TYPE = '酮体' ORDER BY CL_TIME DESC");

            List<SBCL_DATA> bxbList = NovaApplication.fdbmanger.findAllByWhere(SBCL_DATA.class,
                    strWhere + " AND CL_TYPE = '白细胞' ORDER BY CL_TIME DESC");

            List<SBCL_DATA> yxsyList = NovaApplication.fdbmanger.findAllByWhere(SBCL_DATA.class,
                    strWhere + " AND CL_TYPE = '亚硝酸盐' ORDER BY CL_TIME DESC");

            List<SBCL_DATA> ndyList = NovaApplication.fdbmanger.findAllByWhere(SBCL_DATA.class,
                    strWhere + " AND CL_TYPE = '尿胆原' ORDER BY CL_TIME DESC");

            List<SBCL_DATA> dbzList = NovaApplication.fdbmanger.findAllByWhere(SBCL_DATA.class,
                    strWhere + " AND CL_TYPE = '蛋白质' ORDER BY CL_TIME DESC");

            List<SBCL_DATA> dhsList = NovaApplication.fdbmanger.findAllByWhere(SBCL_DATA.class,
                    strWhere + " AND CL_TYPE = '胆红素' ORDER BY CL_TIME DESC");

            List<SBCL_DATA> pttList = NovaApplication.fdbmanger.findAllByWhere(SBCL_DATA.class,
                    strWhere + " AND CL_TYPE = '葡萄糖' ORDER BY CL_TIME DESC");

            List<SBCL_DATA> bzList = NovaApplication.fdbmanger.findAllByWhere(SBCL_DATA.class,
                    strWhere + " AND CL_TYPE = '比重' ORDER BY CL_TIME DESC");

            List<SBCL_DATA> phLIST = NovaApplication.fdbmanger.findAllByWhere(SBCL_DATA.class,
                    strWhere + " AND CL_TYPE = 'PH' ORDER BY CL_TIME DESC");

            if (wssList != null && wssList.size() > 0) {
                tv_khxs.setText(wssList.get(0).getCL_VALUE());
                tv_ny_time.setText("" + wssList.get(0).getCL_TIME());
            } else {
                tv_khxs.setText("");
            }
            if (yxList != null && yxList.size() > 0) {
                tv_yx.setText(yxList.get(0).getCL_VALUE());
            } else {
                tv_yx.setText("");
            }
            if (ttList != null && ttList.size() > 0) {
                tv_tt.setText(ttList.get(0).getCL_VALUE());
            } else {
                tv_tt.setText("");
            }
            if (bxbList != null && bxbList.size() > 0) {
                tv_bxb.setText(bxbList.get(0).getCL_VALUE());
            } else {
                tv_bxb.setText("");
            }
            if (yxsyList != null && yxsyList.size() > 0) {
                tv_yxsy.setText(yxsyList.get(0).getCL_VALUE());
            } else {
                tv_yxsy.setText("");
            }
            if (ndyList != null && ndyList.size() > 0) {
                tv_ndy.setText(ndyList.get(0).getCL_VALUE());
            } else {
                tv_ndy.setText("");
            }
            if (dbzList != null && dbzList.size() > 0) {
                tv_dbz.setText(dbzList.get(0).getCL_VALUE());
            } else {
                tv_dbz.setText("");
            }
            if (dhsList != null && dhsList.size() > 0) {
                tv_dhs.setText(dhsList.get(0).getCL_VALUE());
            } else {
                tv_dhs.setText("");
            }
            if (pttList != null && pttList.size() > 0) {
                tv_ptt.setText(pttList.get(0).getCL_VALUE());
            } else {
                tv_ptt.setText("");
            }
            if (bzList != null && bzList.size() > 0) {
                tv_bz.setText(bzList.get(0).getCL_VALUE());
            } else {
                tv_bz.setText("");
            }
            if (phLIST != null && phLIST.size() > 0) {
                tv_ph.setText(phLIST.get(0).getCL_VALUE());
            } else {
                tv_ph.setText("");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void egg() {
        try {
//            List<SBCL_DATA> egg= NovaApplication.fdbmanger.findAllByWhere(SBCL_DATA.class,
//                    strWhere + " AND  (CL_TYPE = '理邦心电图片' or CL_TYPE = '心电图片' )",
//                    "CL_TIME desc");
//            if(egg != null && egg.size()>0){
//                iv_tjbg_egg.setVisibility(View.VISIBLE);
//                tv_tjbg_egg_nodata.setVisibility(View.GONE);
//                Log.e("心电图片地址",egg.get(0).getCL_VALUE()+"***********");
//                Bitmap bitmap = getLocalBitmap(egg.get(0).getCL_VALUE());
//                iv_tjbg_egg.setImageBitmap(bitmap);
//            } else {
//                iv_tjbg_egg.setVisibility(View.GONE);
//                tv_tjbg_egg_nodata.setVisibility(View.VISIBLE);
//                tv_tjbg_egg_nodata.setText("暂无心电体检数据");
//            }

            List<SBCL_DATA> eggText = NovaApplication.fdbmanger.findAllByWhere(SBCL_DATA.class,
                    strWhere + " AND CL_TYPE = '心电诊断结果' ORDER BY CL_TIME DESC");
//            List<SBCL_DATA> eggData = NovaApplication.fdbmanger.findAllByWhere(SBCL_DATA.class,
//                    strWhere + " AND CL_TYPE = '心率' ORDER BY CL_TIME DESC");

            if (eggText != null && eggText.size() > 0) {
                tv_tjbg_egg.setVisibility(View.VISIBLE);
                tv_tjbg_egg_time.setVisibility(View.VISIBLE);
                tv_tjbg_egg.setText("心电诊断结果:" + eggText.get(0).getCL_VALUE());
                tv_tjbg_egg_time.setText(eggText.get(0).getCL_TIME()+"");
            } else {
                tv_tjbg_egg.setVisibility(View.VISIBLE);
                tv_tjbg_egg_time.setVisibility(View.GONE);
                tv_tjbg_egg.setText("暂无心电体检数据");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public Bitmap getLocalBitmap(String url) {
        try {
            FileInputStream fis = new FileInputStream(url);
            return BitmapFactory.decodeStream(fis);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static void saveCLData(String device_name, String device_type, String cl_type, String cl_value,
                                  String cl_type_name, String GUID, String IDCARD, String cl_time) {
        SBCL_DATA sbcl_Data = new SBCL_DATA();
        sbcl_Data.setDEVICE_NAME(device_name);
        sbcl_Data.setDEVICE_TYPE(device_type);
        sbcl_Data.setCL_TYPE(cl_type);
        sbcl_Data.setCL_VALUE(cl_value);
        sbcl_Data.setCL_TYPE_NAME(cl_type_name);
        sbcl_Data.setGUID(GUID);
        sbcl_Data.setIDCARD(IDCARD);
        sbcl_Data.setCL_TIME(cl_time);
        sbcl_Data.setIS_UPDATE("Y");
        NovaApplication.fdbmanger.save(sbcl_Data);
    }

}
